package com.hutao.manage.service.impl;


import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.enums.RaNrs2000FileStatuEnum;
import com.hutao.manage.mapper.RaNrs2002PdfLogMapper;
import com.hutao.manage.mapper.ScanMapper;
import com.hutao.manage.pojo.RaNrs2002PdfLog;
import com.hutao.manage.pojo.Scan;
import com.hutao.manage.service.RaNrs2000Service;
import com.hutao.manage.util.CopyFile;
import com.hutao.manage.util.DownFileUtil;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * ClassName:RaNrs2000ServiceImpl
 * Package：com.slmr.archive.service.impl
 * Description:
 *
 * @Date:2020-09-18 16:18
 * @Author:liyutao
 */
@Service
public class RaNrs2000ServiceImpl implements RaNrs2000Service {

    @Autowired
    private ScanMapper scanMapper;

    @Autowired
    private RaNrs2002PdfLogMapper raNrs2002PdfLogMapper;

    @Transactional
    @Override
    public String synchronizedRaNrs2000DataTo55(Long raNrs2000Id) {
        String orgcode="49557184-0";
        List<RaNrs2002PdfLog> raNrs2002PdfLogs = raNrs2002PdfLogMapper.getUnSynchronizedFilePdfLog(RaNrs2000FileStatuEnum.GENERATE_FILE.getValue().toString(),raNrs2000Id);
        if(CollectionUtils.isEmpty(raNrs2002PdfLogs)){
            return "住院患者营养风险筛查 NRS-2002查询已生成文件信息为空!";
        }

        StringBuilder builder = new StringBuilder("住院患者营养风险筛查 NRS-2002表ID:");
        for (RaNrs2002PdfLog raNrs2002PdfLog:raNrs2002PdfLogs){
            String sizeTemp="01";
           /* if(icfList!=null&&icfList.size()>0){
                size=icfMapper.getIcfSize(icf.getId(),icf.getOnlyId());
                if(size<10){
                    sizeTemp="0"+size;
                }else{
                    sizeTemp=""+size;
                }
                // size=scanMapper.selectMax(orgcode,"40",icfList.get(0).getOnlyId());
            }*/
            String onlyId=raNrs2002PdfLog.getOnlyId();
            //生成文件访问路径
            String imgUrl=raNrs2002PdfLog.getImgPath();
            String mrPath = "/"
                    + onlyId.substring(onlyId.length() - 4,
                    onlyId.length() - 2) + "/"
                    + onlyId.substring(onlyId.length() - 2) + "/" + onlyId;

            try {
                CopyFile.folderExists("E:/MrDoc/" + orgcode + "/source"
                        + mrPath);
                CopyFile.folderExists("E:/MrDoc/" + orgcode + "/thumbnail"
                        + mrPath);
                String arr[] = {"E:/MrDoc/" + orgcode + "/source"
                        + mrPath + "/", "E:/MrDoc/" + orgcode + "/thumbnail"
                        + mrPath + "/"};
                // List<String> imgList= PDFUtil.pdf2Image(pdf,arr);

                String fileName = DownFileUtil.downFileByUrl(imgUrl, arr);
                Integer page = 1;


                Scan scan = new Scan();
                if (page < 10) {
                    scan.setJlh("1090" + sizeTemp + "0" + page);
                } else {
                    scan.setJlh("1090" + sizeTemp + page);
                }
                scan.setZzdm(orgcode);
                scan.setZzname("山东省立医院");
                scan.setBiaoshima(onlyId);
                scan.setBinganhao(raNrs2002PdfLog.getMrId());
                scan.setName(raNrs2002PdfLog.getName());
                scan.setBiaomilv("2");
                scan.setPagename("90");
                scan.setSyspath("/MrDoc");
                scan.setGuidangpath(mrPath);
                scan.setFilename(fileName);
                scan.setUserScan("000000000000000001");
                scan.setUserCatalogue("000000000000000001");
                scan.setSource("1");
                scan.setSerialno(UUID.randomUUID().toString());
                scan.setPid(raNrs2002PdfLog.getParentNumber().toString());
                scan.setStatus((long) 0);
                scanMapper.insert(scan);


                raNrs2002PdfLog.setStatus(RaNrs2000FileStatuEnum.SYNCHRONIZE_FILE.getValue().toString());
                raNrs2002PdfLog.setUpdateTime(new Date());
                builder.append(raNrs2002PdfLog.getParentNumber()).append("、");
                raNrs2002PdfLogMapper.updateByPrimaryKeySelective(raNrs2002PdfLog);

            }catch(Exception e){
                e.printStackTrace();
                return "同步营养评估数据失败,失败原因:"+e.getMessage();
            }
        }
        builder.append("同步文件数据成功!");
        return builder.toString();
    }
}
